﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TestSystem.BLL.TestSystem;


public partial class Admin_ManageTests : System.Web.UI.Page
{
    private int quizID = 0;
    public int QuizID
    {
        get
        {
            if (quizID == 0 && Request.QueryString["quizID"] != null)
                quizID = Convert.ToInt32(Request.QueryString["quizID"]);
            return quizID;
        }
    }
    private List<Test> tests;
    public List<Test> Tests
    {
        get
        {
            if (tests == null)
                tests = (List<Test>)testsObjSource.Select();
            return tests;
        }
        set
        {
            tests = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var ddlComplexity = lvwTests.FindControl("ddlComplexity") as DropDownList;
            if (ddlComplexity != null)
            {
                var data = (from test in Tests
                           group test by test.Complexity
                           into groups
                               select new {Complexity = groups.Key}).ToList();
                ddlComplexity.DataSource = data;
                ddlComplexity.DataBind();
            }

            var ddlType = lvwTests.FindControl("ddlType") as DropDownList;
            if(ddlType != null)
            {
                var data = (from test in Tests
                            group test by test.TestType
                            into groups
                                select new {TestType = groups.Key}).ToList();
                ddlType.DataSource = data;
                ddlType.DataBind();
            }
        }
    }

    protected void testsObjSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        if (QuizID == 0)
            e.Cancel = true;
        e.InputParameters["quizID"] = QuizID;
    }

    protected void testsObjSource_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        if (QuizID == 0)
            e.Cancel = true;
        e.InputParameters["quizID"] = QuizID;
    }

    protected void lvwTests_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        e.Cancel = true;
        int testId = Convert.ToInt32(e.Keys["TestID"]);
        var currTest = Tests.Find(tst => tst.TestID == testId);
        currTest.Body = e.NewValues["Body"] == null ? null : e.NewValues["Body"].ToString();
        currTest.TestOrder = Convert.ToInt32(e.NewValues["TestOrder"]);
        currTest.TestType = Convert.ToInt32(e.NewValues["TestType"]);
        currTest.Complexity = Convert.ToInt32(e.NewValues["Complexity"]);
        currTest.Update();
        lvwTests.EditIndex = -1;
        lvwTests.DataBind();
        Tests = null;
    }

    protected void lvwTests_ItemInserted(object sender, ListViewInsertedEventArgs e)
    {
        Tests = null;
    }

    protected void lvwTests_ItemDeleted(object sender, ListViewDeletedEventArgs e)
    {
        Tests = null;
    }

    protected void lvwTests_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "AddAnswers")
        {
            int testId = Convert.ToInt32(e.CommandArgument);
            int quizId = QuizID;
            Response.Redirect("ManageAnswers.aspx?testID=" + testId + "&quizID=" + quizId + "#insert");
        }
        if (e.CommandName == "ShowAllAnswers")
        {
            int testId = Convert.ToInt32(e.CommandArgument);
            int quizId = QuizID;
            Response.Redirect("ManageAnswers.aspx?testID=" + testId + "&quizID=" + quizId);
        }
    }

    protected void btnFilter_Click(object sender, EventArgs e)
    {
        var ddlComplexity = lvwTests.FindControl("ddlComplexity") as DropDownList;
        var ddlType = lvwTests.FindControl("ddlType") as DropDownList;
        if(ddlComplexity != null && ddlType != null)
        {
            int complexity = Convert.ToInt32(ddlComplexity.SelectedValue);
            int type = Convert.ToInt32(ddlType.SelectedValue);
            if (testsObjSource.SelectParameters.Count == 3)
            {
                testsObjSource.SelectParameters.RemoveAt(1);
                testsObjSource.SelectParameters.RemoveAt(1);
                testsObjSource.SelectParameters.Add("complexityFilter", complexity.ToString());
                testsObjSource.SelectParameters.Add("typeFilter", type.ToString());
            }
            testsObjSource.DataBind();
        }
    }

    protected void btnAll_Click(object sender, EventArgs e)
    {
        if (testsObjSource.SelectParameters.Count == 3)
        {
            testsObjSource.SelectParameters.RemoveAt(1);
            testsObjSource.SelectParameters.RemoveAt(1);
            testsObjSource.SelectParameters.Add("complexityFilter", null);
            testsObjSource.SelectParameters.Add("typeFilter", null);
        }
        testsObjSource.DataBind();
    }
}
